package com.test.ddaidai.main_account;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.test.ddaidai.BaseActivity;
import com.test.ddaidai_android.activity.R;

public class BaseTabActivity  extends ActivityGroup  {
	private ViewPager viewPager;
	private ArrayList<View> pageViews;
	TextView tab_header_left, tab_header_center, tab_header_right;
	ImageView youbiao;
	Animation am;
	int w;
	float prearg1 = 0;
	float predree = 0.0f;
	int current = 0;

	private Class [] clazzs;

	public void setActivityClass(Class [] clazzs){
		this.clazzs = clazzs;
	}

	public void setHeaderTextVal(String tab_header_left_txt,String tab_header_center_txt,String tab_header_right_txt){
		tab_header_left.setText(tab_header_left_txt);
		tab_header_center.setText(tab_header_center_txt);
		tab_header_right.setText(tab_header_right_txt);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_basetab);
		viewPager = (ViewPager) findViewById(R.id.pager);
		youbiao = (ImageView) findViewById(R.id.youbiao);

		tab_header_left = (TextView) findViewById(R.id.tab_header_left);
		tab_header_center = (TextView) findViewById(R.id.tab_header_center);
		tab_header_right = (TextView) findViewById(R.id.tab_header_right);
		TextView btns[] = { tab_header_left, tab_header_center, tab_header_right};
		InItView(clazzs);
		viewPager.setAdapter(new myPagerView());

		ScreenInfo s = new ScreenInfo(this);

		w = s.getWidth() /3;
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) youbiao
				.getLayoutParams();
		lp.width = w;
		youbiao.setLayoutParams(lp);

		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				selsct(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				if (arg1 != 0) {
					TranslateAnimation animation = new TranslateAnimation(
							predree * w + current, arg1 * w + arg0 * w, 0, 0);
					animation.setDuration(200);
					animation.setFillAfter(true);
					youbiao.startAnimation(animation);
					predree = arg1;
					current = arg0 * w;
				}
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		for (int i = 0; i < 3; i++) {
			final int a = i;
			btns[a].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					viewPager.setCurrentItem(a);
					selsct(a);
				}
			});
		}
		selsct(0);
	}

	private void selsct(int i) {
		TextView btns[] = { tab_header_left, tab_header_center, tab_header_right };
		for (int n = 0; n < 3; n++) {
			final int a = n;
			if (i == a) {
				btns[a].setTextColor(Color.parseColor("#FF8040"));
			} else {
				btns[a].setTextColor(Color.parseColor("#d6d6d6"));
			}
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	void InItView(Class [] clazzs) {
		pageViews = new ArrayList<View>();
		View [] views = new View[clazzs.length];
		for (int i = 0; i < clazzs.length; i++) {
			View view = getLocalActivityManager().startActivity(
					"activity00",
					new Intent(this,
							clazzs[i]))
							.getDecorView();
			views[i] = view;
		}

		for (int i = 0; i < views.length; i++) {
			pageViews.add(views[i]);
		}
	}

	class myPagerView extends PagerAdapter {
		// 显示数目
		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(pageViews.get(arg1));
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(pageViews.get(arg1));
			return pageViews.get(arg1);
		}
	}

	/**
	 * 设置头部UI
	 */
	private TextView mTextTitle;

	public void setTitle(String title) {
		if (mTextTitle == null) {
			mTextTitle = (TextView) findViewById(R.id.text_title);
		}

		if (mTextTitle != null) {
			mTextTitle.setText(title);
		}
	}

	public void setTitle(String title, int leftBtnRes, boolean isLeftText,int rightBtnRes,boolean isRightText) {
		setTitle(title);

		ImageButton btnLeft = (ImageButton) findViewById(R.id.btn_header_left);
		ImageButton btnRight = (ImageButton) findViewById(R.id.btn_header_right);
		TextView mHeaderLeftText = (TextView) findViewById(R.id.tv_header_left);
		TextView mHeaderRightText = (TextView) findViewById(R.id.tv_header_right);
		if (leftBtnRes != 0) {
			if(isLeftText){
				mHeaderLeftText.setText(getString(leftBtnRes));
				btnLeft.setVisibility(View.GONE);
				mHeaderLeftText.setVisibility(View.VISIBLE);
			}else{
				btnLeft.setImageResource(leftBtnRes);
				mHeaderLeftText.setVisibility(View.GONE);
				btnLeft.setVisibility(View.VISIBLE);
			}
		} else {
			btnLeft.setVisibility(View.GONE);
			mHeaderLeftText.setVisibility(View.GONE);
		}

		if (rightBtnRes != 0) {
			if(isRightText){
				mHeaderRightText.setText(getString(rightBtnRes));
				btnRight.setVisibility(View.GONE);
				mHeaderRightText.setVisibility(View.VISIBLE);
			}else{
				btnRight.setImageResource(rightBtnRes);
				mHeaderRightText.setVisibility(View.GONE);
				btnRight.setVisibility(View.VISIBLE);
			}
		} else {
			btnRight.setVisibility(View.GONE);
		}
	}

	/**
	 * Header 左边按钮点击事件
	 * 
	 * @param view
	 */
	public void onBtnLeft(View view) {
		finish();
	}

	/**
	 * Header 右边按钮点击事件
	 * 
	 * @param view
	 */
	public void onBtnRight(View view) {
	}

}
